import https from 'https'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// 从 dataSource 中提取的 fileUrl 列表
const dataSource = [
  {
    description: '会籍商品列表',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-18_044652/membershipGoodsMgmt.json',
  },
  {
    description: '核销商品列表',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-25_083028/qrcodeGoodsMgmt.json',
  },
  {
    description: '预约商品列表',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-23_084545/reserveGoodsMgmt.json',
  },
  {
    description: '权益商品列表',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-22_021950/equityGoodsMgmt.json',
  },
  {
    description: '权益商品',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-15_035551/equityGoods.json',
  },
  {
    description: '登录页设置',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-26_062625/loginSet.json',
  },
  {
    description: '测试页面',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-30_074341/test.json',
  },
  {
    description: '接口设置',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-25_034830/interfaceConfig.json',
  },
  {
    description: '酒店订单列表',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-26_021637/hotel.json',
  },
  {
    description: '核销订单详情',
    fileUrl: 'https://oss-images.dragonpass.com.cn/business-saas/platform/2025-09-27_152019/detail.json',
  },
]

function fetchJson(url) {
  return new Promise((resolve, reject) => {
    https
      .get(url, (res) => {
        let data = ''
        res.on('data', (chunk) => {
          data += chunk
        })
        res.on('end', () => {
          try {
            const jsonData = JSON.parse(data)
            resolve(jsonData)
          } catch (error) {
            reject(new Error(`Failed to parse JSON from ${url}: ${error.message}`))
          }
        })
      })
      .on('error', (error) => {
        reject(new Error(`Failed to fetch ${url}: ${error.message}`))
      })
  })
}

async function fetchAllSchemas() {
  const schemas = {}

  for (const item of dataSource) {
    try {
      console.log(`Fetching ${item.description}...`)
      const schema = await fetchJson(item.fileUrl)

      schemas[item.description] = schema

      console.log(`✓ Successfully fetched ${item.description}`)
    } catch (error) {
      console.error(`✗ Failed to fetch ${item.fileUrl}:`, error.message)
    }
  }
  console.log(schemas, 88888)
  return schemas
}

// 执行获取
fetchAllSchemas()
  .then((schemas) => {
    console.log('\n=== 获取到的 Schema 数据 ===')
    console.log('Keys:', Object.keys(schemas))

    // 生成 TypeScript 文件内容
    const tsContent = `import { PageSchema } from './types.js'

// 原始数据源信息（用于参考）
export const dataSource = ${JSON.stringify(dataSource, null, 2)}

// 基于 dataSource fileUrl 生成的 Schema 数据
export const fileUrlSchemas: Record<string, PageSchema> = ${JSON.stringify(schemas, null, 2)}

// 演示用的Schema数据 - 基于真实业务场景的Formily Schema
export const demoSchemas: Record<string, PageSchema> = fileUrlSchemas
`

    // 将结果写入文件
    const outputPath = path.join(__dirname, '../src/demo-schemas.ts')
    fs.writeFileSync(outputPath, tsContent)
    console.log(`\n✓ 数据已保存到: ${outputPath}`)
  })
  .catch((error) => {
    console.error('Error:', error)
  })

